package com.astn.bks;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import com.astn.bks.util.BksConstants;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteFacility;
import com.astn.entity.booking.QuoteRoomType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class DiaFacility extends Activity {
	private Spinner spnFacility;
	private Button btnOK;
	private Button btnCancel;
	private List<QuoteFacility> lstFacilities;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diafacility);
		getWindow().setLayout(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		btnOK = (Button) findViewById(R.id.btnOK);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		spnFacility = (Spinner) findViewById(R.id.spnFacility);

		lstFacilities = getListFacilities();
		ArrayAdapter<QuoteFacility> facilityAdapter = new ArrayAdapter<QuoteFacility>(
				this, android.R.layout.simple_spinner_item, lstFacilities);
		facilityAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnFacility.setAdapter(facilityAdapter);
		btnOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent data = new Intent();
				data.putExtra(BksConstants.EXTRA_FACILITY,
						(QuoteFacility) spnFacility.getSelectedItem());
				setResult(RESULT_OK, data);
				finish();
			}

		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent data = new Intent();
				setResult(Activity.RESULT_CANCELED, data);
				finish();
			}

		});

	}

	private List<QuoteFacility> getListFacilities() {
		List<QuoteFacility> lstTemp = new Vector<QuoteFacility>();
		// lstTemp.add(new QuoteFacility(1, "Spa in room"));
		// lstTemp.add(new QuoteFacility(2, "Free internet"));
		// lstTemp.add(new QuoteFacility(3, "Free telephone"));
		// lstTemp.add(new QuoteFacility(4, "Television"));
		// lstTemp.add(new QuoteFacility(5, "Gym"));
		Object rsObj = null;

		try {
			rsObj = WebserviceUtils.call("com_xmlrpc", "getListFacility", null);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (rsObj != null) {
			QuoteFacility[] arrRs = null;
			try {
				arrRs = JsonUtils.gson.fromJson((String) rsObj,
						QuoteFacility[].class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (arrRs != null) {
				lstTemp.addAll(Arrays.asList(arrRs));
			}
		}
		return lstTemp;
	}

}
